package org.apogee.mil.client.main.pages.trainee.forms;

import java.util.List;

import org.apogee.mil.shared.Question;

import br.com.freller.tool.client.Print;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;

public class ReviewForm extends LayoutContainer {

	private FormData data;
	private List<Question> questions;
	private TextArea area;

	public ReviewForm(List<Question> wrongQuestions) {
		this.questions = wrongQuestions;
	}

	public void onRender(Element parent, int index) {
		super.onRender(parent, index);

		data = new FormData("100%");

		FormPanel form = new FormPanel();
		form.setHeading("Review");  
		form.setFrame(true);
		form.setBorders(true);
		form.setSize(600, 600);
		form.setBodyBorder(true);
		form.setHideLabels(true);
		form.setButtonAlign(HorizontalAlignment.CENTER);

		area = new TextArea();
		area.setHeight(500);
		area.setReadOnly(true);
		area.setValue("");

		form.add(area, data);

		Button print = new Button("Print");
		print.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				print();

			}
		});

		form.addButton(print);
		
		add(form);
		layout();

		getQuestions();
	}

	private void getQuestions() {
		StringBuilder sb = new StringBuilder("Review Missed Questions:\n");

		for(Question q : questions) {
			sb.append(q.toString() + "\n\n");
		}

		area.setValue(sb.toString());
		area.repaint();

	}

	private void print(){

		StringBuilder sb = new StringBuilder("<p style='color:black; font-size:24pt;'>Review Missed Questions:</p><br/>");
		
		for (Question q : questions) {
			sb.append(q.toHTMLString());
		}
		
		Print.it(sb.toString());
 	}

}
